package com.my.hzb.order.service;

import java.util.*;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.my.hzb.common.bean.BaseResultBean;
import com.my.hzb.common.bean.GlobalBean;
import com.my.hzb.common.bean.ResultBean;
import com.my.hzb.common.util.HttpUtil;
import com.my.hzb.common.util.JsonUtil;
import com.my.hzb.entiy.Address;
import com.my.hzb.entiy.Product;

import sun.rmi.runtime.Log;

public class OrderHttpServiceImpl {

	private static final Logger log = LogManager.getLogger(OrderHttpServiceImpl.class);
	
	static String userurl = GlobalBean.Url.user;
	static String prourl = GlobalBean.Url.product;
	public Address getAdsByName(String userName){
		Address ads = new Address();
		try{
			Map<String,String> keyValueMap = new HashMap<String,String>();
			keyValueMap.put("userName", userName);
			ResultBean resultBean = getResultByHttp(userurl+"ads/getByUserName",keyValueMap);
			if(ResultBean.RETURN_SUCCESS_CODE.equals(resultBean.getRespCode())){
				ads = JsonUtil.g.fromJson(resultBean.getReturnStr(), Address.class);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return ads;
	}
	public Product getHotPro(){
		Product pro = new Product();
		try{
			Map<String,String> keyValueMap = new HashMap<String,String>();
			ResultBean resultBean = getResultByHttp(prourl+"pro/getHot",keyValueMap);
			if(ResultBean.RETURN_SUCCESS_CODE.equals(resultBean.getRespCode())){
				pro = JsonUtil.g.fromJson(resultBean.getReturnStr(), Product.class);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return pro;
	}
	public Product isPro(Integer proId){
		Product pro = new Product();
		try{
			Map<String,String> keyValueMap = new HashMap<String,String>();
			keyValueMap.put("id", proId+"");
			ResultBean resultBean = getResultByHttp(prourl+"pro/getOne",keyValueMap);
			if(ResultBean.RETURN_SUCCESS_CODE.equals(resultBean.getRespCode())){
				pro = JsonUtil.g.fromJson(resultBean.getReturnStr(), Product.class);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return pro;
	}
	
	public boolean updateStock(Integer proId, Integer type, Integer stock) {
		try{
			if(null!=proId&&null!=type&&null!=stock){
				Map<String,String> keyValueMap = new HashMap<String,String>();
				keyValueMap.put("proId", proId+"");
				keyValueMap.put("type", type+"");
				keyValueMap.put("stock", stock+"");
				ResultBean resultBean = getResultByHttp(prourl+"pro/updateStock",keyValueMap);
				if(null!=resultBean&&"0000".equals(resultBean.getRespCode())){
					if(Integer.parseInt(resultBean.getReturnStr())>0){
						return true;
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	public static ResultBean getResultByHttp(String url,Map<String,String> keyValueMap){
		String result = HttpUtil.requestPost(url, keyValueMap, "utf-8", 70000);
		log.info("url="+url+",params="+keyValueMap+",result="+result);
		ResultBean resultBean = JsonUtil.g.fromJson(result, ResultBean.class);
		return resultBean;
	}
	
}
